from 读取文件 import *

from Curve_fitting import *
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10, 10))
ax = plt.gca()


def Jkx_std(Y, n=6):  # 理论渐开线中, x与y的关系
    t = np.linspace(1.08, 0, 1000)
    r = 50
    w = 1
    x = r * (np.cos(w * t + 1.082) + w * t * np.sin(w * t + 1.082))
    x = x - x[0]
    y = r * (np.sin(w * t + 1.082) - w * t * np.cos(w * t + 1.082))
    y = y - y[0]
    re1 = CF(y, x, n=n)["Factor"]
    return np.polyval(re1, Y)  # 返回一个多项式,关于Y的


filename = "正向仿真数据/7v008.txt"
filename = np.array(loadDatadet(filename))
x_0 = 0
y_0 = 0

x = filename[:, 0] - x_0
y = filename[:, 1] - y_0  # 平移到以原点为中心
# print(x)
# plt.scatter(x, y, 0.1, color='red')

x_real = []  # 表示筛选的点
y_real = []

# plt.scatter(x, y, s=0.1)
for date in np.arange(0, len(x) - 1, 1):
    if -26 <= y[date] <= -5:
        if x[date] <= Jkx_std(y[date]):
            x_real.append(x[date])
            y_real.append(y[date])
            pass
    pass

# plt.scatter(x_real, y_real, s=0.2)
Cf = CF(y_real, x_real, n=8)  # Cf是一个字典,包含了因数和拟合误差
# print("Error:")
# print(Cf["Error"])
s = np.linspace(0, 24, 100)
# plt.plot(np.polyval(Cf["Factor"], s), s, color='red')
# 对比求误差

Anger = np.linspace(1.08, 0, 1000)
r = 50
X = r * np.cos(1.082 + Anger) + r * Anger * np.sin(Anger + 1.082)
X = X - X[0]
Y = r * np.sin(1.082 + Anger) - r * Anger * np.cos(Anger + 1.082)
Y = Y - Y[0]
Error = np.abs(np.polyval(Cf["Factor"], Y) - X)
print(Error)
plt.plot(Anger,Error)
# c1=CF(Error,Anger,3)["Factor"]
# print(c1)
# plt.plot(Anger,np.polyval(c1,Anger))
# print(Error)
# print(np.max(Error) - np.mean(Error))
# plt.xlim(-15, 15)
# plt.ylim(-30, 0)
ax.grid()
# plt.scatter(X, Y, 0.1)
# plt.rcParams['saving.dpi'] = 1000  # 图片像素
# plt.rcParams['figure.dpi'] = 1000
plt.show()
